<?php

namespace App\Admin\Controllers;

use App\Models\ClassCourse;
use App\Models\Course;
use App\Models\Grade;
use App\Models\Teacher;
use App\Models\Semester;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;



class ClassCourseController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('教务管理')
            ->description('教学计划信息')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('教务管理')
            ->description('教学计划详细信息')
            ->body($this->detail($id));

    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
        ->header('教务管理')
        ->description('编辑教学计划')
        ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('教务管理')
            ->description('新建教学计划')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ClassCourse);

        $grid->id('Id')->sortable();
        $grid->column('semester.name','学期');
        $grid->column('class.class_name','班级');
        $grid->column('course.course_name','课程');

        $grid->course_teacher('任课老师')->display(function ($teacher_id) {
            $teacher = Teacher::where('id', $teacher_id)->first();
            return $teacher->name;
        });

        $states = [
            'on'  => ['value' => 1, 'text' => '是', 'color' => 'primary'],
            'off' => ['value' => 2, 'text' => '否', 'color' => 'default'],
        ];
        $grid->end('是否结课')->switch($states);

        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->column(1/2, function ($filter) {
                $semesters = Semester::get();
                $semester_arr = [];
                foreach ($semesters as $semester) {
                    $semester_arr[$semester->id] = $semester->name;
                }

                $grades = Grade::get();
                $grade_arr = [];
                foreach ($grades as $grade) {
                    $grade_arr[$grade->id] = $grade->class_name;
                }

                $courses = Course::get();
                $course_arr = [];
                foreach ($courses as $course) {
                    $course_arr[$course->id] = $course->course_name;
                }

                $filter->equal('semester_id', '学期')->select($semester_arr);
                $filter->equal('class_id', '班级名称')->select($grade_arr);
                $filter->equal('course_id','课程名称')->select($course_arr);
            });

            $filter->column(1/2, function ($filter) {
                $teachers = Teacher::where('on_duty','=', 0)->get();
                $teacher_arr = [];

                foreach ($teachers as $teacher) {
                    $teacher_arr[$teacher->id] = $teacher->name;
                }

                $filter->equal('course_teacher', '任课教师')->select($teacher_arr);

                $filter->equal('end', '是否结课')->radio([
                    0    => ' 否',
                    1    => ' 是',
                ]);
            });

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ClassCourse::findOrFail($id));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ClassCourse());

        $teachers = Teacher::where('on_duty','=', 0)->get();
        $teacher_arr = [];

        foreach ($teachers as $teacher) {
            $teacher_arr[$teacher->id] = $teacher->name;
        }

        $grades = Grade::get();
        $grade_arr = [];

        foreach ($grades as $grade) {
            $grade_arr[$grade->id] = $grade->class_name;
        }

        $courses = Course::get();
        $course_arr = [];

        foreach ($courses as $course) {
            $course_arr[$course->id] = $course->course_name;
        }

        $semesters = Semester::orderBy('is_semester', 'desc')->get();
        $semester_arr = [];
        foreach ($semesters as $semester) {
            $semester_arr[$semester->id] = $semester->name;
        }

        $form->select('semester_id', trans('选择学期'))->options($semester_arr)->setWidth(3, 2);
        $form->select('class_id', trans('选择班级'))->options($grade_arr)->setWidth(3, 2);
        $form->select('course_id', trans('选择课程'))->options($course_arr)->setWidth(3, 2);
        $form->select('course_teacher', trans('任课教师'))->options($teacher_arr)->setWidth(3, 2);
        $states = [
            'on'  => ['value' => 1, 'text' => '是'],
            'off' => ['value' => 0, 'text' => '否'],
        ];

        $form->switch('end','是否节课')->states($states);

        $form->tools(function (Form\Tools $tools) {
            // 去掉`查看`按钮
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });

        return $form;
    }
}
